package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.beautyescort.EscortBeautyDispatch;
import com.yanqu.road.entity.beautyescort.rank.EscortWeekUserValue;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eItemId;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogBeautyEscortRob;
import com.yanqu.road.entity.log.LogBeautySkillExp;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.beautyescort.BeautyEscortModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.beautyescort.BeautyEscortMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_BEAUTY_ESCORT_ROB_CART_FROM_CROSS, desc = "劫镖-跨服回包")
public class BeautyEscortRobCartCmdFromCross extends AbstractCommand {

    BeautyEscortProto.CrossBeautyEscortRobCartRespMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = BeautyEscortProto.CrossBeautyEscortRobCartRespMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        //购买次数的消耗扣除
        if(reqMsg.hasBuyConsume()){
            Property consume  = PropertyHelper.parseStringToProperty(reqMsg.getBuyConsume());
            if(consume != null){
                //还原预扣记录
                player.getModule(PreReduceModule.class).restorePreReduce(PropertyHelper.parsePropertyToString(consume));

                //扣除消耗
                if(reqMsg.getRet() == 0) {
                    player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.BeautyEscort, eLogMoneyType.BeautyEscortRobDispatchConsume);
                }
            }
        }

        List<EscortBeautyDispatch> dispatchList = BeautyEscortMgr.parseBeautyDispatch(reqMsg.getBeautyList());
        if(reqMsg.getRet() != 0){
            ret = reqMsg.getRet();
            player.getModule(BeautyEscortModule.class).restoreRobTimes(dispatchList, reqMsg.getBuyBeautyList());
            return;
        }

        //胜利+奖励
        String reward = reqMsg.getReward();
        long allExp = 0;
        boolean win = false;
        List<LogBeautySkillExp> logList = new ArrayList<>();
        if(reqMsg.hasWin() && reqMsg.getWin()){
            win = true;
            List<UserBeauty> userBeautyList = new ArrayList<>();
            BeautyModule beautyModule = player.getModule(BeautyModule.class);
            long exp = PropertyHelper.parseStringToProperty(reward).getCountByGoodsId(eItemId.BEAUTY_ESCORT_EXP_GOODS.getValue()).longValue();
            for (BeautyEscortProto.BeautyEscortBeautyDispatchDataTemp dispatchDataTemp : reqMsg.getBeautyList()) {
                UserBeauty userBeauty = beautyModule.getUserBeauty(dispatchDataTemp.getBeautyId());
                if(userBeauty != null){
                    synchronized(userBeauty) {
                        beautyModule.addBeautyEscortExp(userBeauty, exp);
                        userBeautyList.add(userBeauty);
                        allExp += exp;
                        logList.add(new LogBeautySkillExp(player.getUserId(), userBeauty.getBeautyId(), exp, userBeauty.getEscortExp(), eLogMoneyType.BeautyEscort.getValue(), eLogMoneyType.BeautyEscortRobReward.getValue()));
                    }
                }
            }
            Property goodsReward = PropertyHelper.parseStringToProperty(reward);
            //去掉美女走镖经验道具奖励，因为已记录到美女身上
            goodsReward.removeProperty(eItemId.BEAUTY_ESCORT_EXP_GOODS.getValue());
            player.getModule(CurrencyModule.class).addCurrency(PropertyHelper.parsePropertyToString(goodsReward), eLogMoneyType.BeautyEscort, eLogMoneyType.BeautyEscortRobReward);

            EscortWeekUserValue weekUserValue = BeautyEscortMgr.getEscortWeekUserValueOrInit(BeautyEscortMgr.getThisWeekId(), player.getUserId());
            synchronized (weekUserValue){
                weekUserValue.setValue(allExp + weekUserValue.getValue());
                weekUserValue.setModifyTime(System.currentTimeMillis());
            }
            //美女押镖经验榜单
            player.notifyListener(eGamePlayerEventType.BeautyEscortAddExpRank.getValue(), allExp);
            player.notifyListener(eGamePlayerEventType.BeautyEscortExpAdd.getValue(), allExp);
            player.notifyListener(eGamePlayerEventType.ServerBeautyEscortAddExpRank.getValue(), allExp);
            player.notifyListener(eGamePlayerEventType.OnlyEscortAddExpRank.getValue(), allExp);
            player.notifyListener(eGamePlayerEventType.ServerOnlyEscortAddExpRank.getValue(), allExp);
            //玩家经验冲榜
            BeautyEscortMgr.changeUnionRank(player.getUserInfo().getUnionUid(), player.getUserId(), UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, weekUserValue.getWeekId());

            beautyModule.syncUserBeauty(userBeautyList, false);
        }

        //经验获得日志
        for (LogBeautySkillExp skillExp : logList) {
            AutoLogMgr.add(skillExp);
        }

        //劫镖日志
        AutoLogMgr.add(new LogBeautyEscortRob(player.getUserId(), reqMsg.getEnemyId(), reqMsg.getEnemyCart().getDartId(), dispatchList,
                BeautyEscortMgr.parseBeautyDispatch(reqMsg.getEnemyCart().getBeautyList()), win, reward, reqMsg.getBuyConsume(), allExp));

        BeautyEscortProto.BeautyEscortRobCartRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortRobCartRespMsg.newBuilder();
        respMsg.setRet(reqMsg.getRet());
        respMsg.setReward(reward);
        respMsg.addAllBeauty(reqMsg.getBeautyList());
        if(reqMsg.hasEnemyCart()) {
            respMsg.setEnemyCart(reqMsg.getEnemyCart());
        }
        respMsg.setPosition(reqMsg.getPosition());
        if(reqMsg.hasWin()) {
            respMsg.setWin(reqMsg.getWin());
        }else {
            respMsg.setWin(false);
        }
        if(reqMsg.hasExp()){
            respMsg.setExp(reqMsg.getExp());
        }
        player.sendPacket(Protocol.U_BEAUTY_ESCORT_ROB_CART, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            BeautyEscortProto.BeautyEscortRobCartRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortRobCartRespMsg.newBuilder();
            respMsg.setRet(reqMsg.getRet());
            player.sendPacket(Protocol.U_BEAUTY_ESCORT_ROB_CART, respMsg);
        }
    }
}
